package com.pkk.penguin.frame.know.java.Javacoreknowledge.lock.impl;

import java.util.concurrent.locks.Condition;
import org.junit.Test;

/**
 * @description:
 * @author: peikunkun
 * @create: 2020-01-13 18:12
 **/
public class ReentrantLock {

  /**
   * 锁-公平锁与否
   */
  java.util.concurrent.locks.ReentrantLock lock = new java.util.concurrent.locks.ReentrantLock(true);

  Condition condition = lock.newCondition();


  @Test
  public void Administrator_18_20200117103251() throws InterruptedException {
    System.out.println("欢迎使用单元测试方法【Administrator_18()_20200117103251】");
    System.out.println("此方法测试描述：【】");

    final Thread thread = new Thread(new Runnable() {
      @Override
      public void run() {
        for (int i = 0; i < 10; i++) {
          try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + " is Running");
            Thread.sleep(1000L);
          } catch (InterruptedException e) {
            e.printStackTrace();
          } finally {
            lock.unlock();
          }
        }
      }
    }, "simple-1");

    final Thread thread1 = new Thread(new Runnable() {
      @Override
      public void run() {
        for (int i = 0; i < 10; i++) {
          try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + " is Running");
            Thread.sleep(1000L);
          } catch (InterruptedException e) {
            e.printStackTrace();
          } finally {
            lock.unlock();
          }
        }
      }
    }, "simple-2");

    thread.start();
    thread1.start();

    thread.join();
    thread1.join();

    //等待
    //condition.await();

    //唤醒
    //condition.signal();
  }
}
